package com.lbd99.data.lbd.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lbd99.data.lbd.dao.SysAccountMapper;
import com.lbd99.data.lbd.vo.SysAccount;
import com.lbd99.data.utils.StrUtil;

@Service
public class SysAccountService {

	@Autowired
	SysAccountMapper sysAccountMapper;
	/**
	 *  查询列表
	 * @return
	 */
	public List<SysAccount> selectList(Map<String,Object> map){
		return  sysAccountMapper.selectList(map);
	}

	/**
	 *  查询列表
	 * @return
	 */
	public Map<String,Object> login(Map<String,Object> map,HttpSession httpSession){
		Map<String,Object> map2=new HashMap<String,Object>();
		SysAccount sysAccount=null;
		List<SysAccount> list=sysAccountMapper.selectList(map);
		if (list!=null&& list.size()>0){
			sysAccount=list.get(0);
			map2.put("isSuc", true);
			map2.put("msg", "登录成功!");
			map2.put("vo", sysAccount);
			httpSession.setAttribute("SESSION_INFO_OBJECT", sysAccount);
			httpSession.setAttribute("SESSION_INFO_OBJECT_ADMIN", sysAccount);
		}else{
			map2.put("isSuc", false);
			map2.put("msg", "用户名或密码错误!");
		}
		return  map2;
	}
	
	
	public Map<String,Object> updatePwd(String oldPwd,String newPwd,Integer id){
		Map<String,Object> map=new HashMap<String,Object>();
		//校验原密码
		SysAccount sysAccount = sysAccountMapper.selectByPrimaryKey(id);
		if(!StrUtil.doEncrypt(oldPwd).equals(sysAccount.getPwd())){
			map.put("isSuc", false);
			map.put("msg", "原密码输入错误!");
			return map;
		}
		sysAccount.setUpdateTime(new Date());
		sysAccount.setPwd(StrUtil.doEncrypt(newPwd));
		sysAccountMapper.updateByPrimaryKeySelective(sysAccount);
		map.put("isSuc", true);
		map.put("msg", "密码修改成功!");
		return map;
	}
	
	
	public Map<String, Object> getEmployee(int page,int rows,String phone){
		Map<String, Object> result = new HashMap<String, Object>();
        PageHelper.startPage(page, rows);
        List<Map<String,Object>>  list=sysAccountMapper.getEmployee(phone);
        PageInfo<Map<String,Object>> pages = new PageInfo<Map<String,Object>>(list);
        result.put("total", pages.getTotal());
        result.put("rows", pages.getList());
        return result;
	}
	
	public Map<String, Object> insertEmployee(SysAccount sysAccount){
		sysAccount.setCreateTime(new Date());
		sysAccount.setUpdateTime(new Date());
		sysAccount.setPwd(StrUtil.doEncrypt(sysAccount.getPwd()));
		sysAccount.setTradePwd(StrUtil.doEncrypt(sysAccount.getTradePwd()));
		sysAccount.setCreateBy("-1");
    	sysAccount.setUpdateBy("-1");
		Map<String, Object> result = new HashMap<String, Object>();
		SysAccount sys = sysAccountMapper.getEmployeeByName(sysAccount.getAccount());
		if(sys != null){
			result.put("isSuc",false);
			result.put("msg","用户名已存在");
			return result;
		}
		int num = sysAccountMapper.insertSelective(sysAccount);
		if(num != 1){
			result.put("isSuc",false);
			result.put("msg","操作失败");
			return result;
		}
		result.put("isSuc",true);
		result.put("msg","操作成功");
		return result;
	}
	
    public Map<String, Object> updateEmployee(SysAccount sysAccount){
    	sysAccount.setUpdateTime(new Date());
    	sysAccount.setCreateBy("-1");
    	sysAccount.setUpdateBy("-1");
    	if(sysAccount.getPwd() != null && sysAccount.getPwd() != ""){
    		sysAccount.setPwd(StrUtil.doEncrypt(sysAccount.getPwd()));
    	}
    	if(sysAccount.getTradePwd() != null && sysAccount.getTradePwd()  != ""){
    		sysAccount.setTradePwd(StrUtil.doEncrypt(sysAccount.getTradePwd()));
    	}
    	Map<String, Object> result = new HashMap<String, Object>();
		int num = sysAccountMapper.updateByPrimaryKeySelective(sysAccount);
		if(num != 1){
			result.put("isSuc",false);
			result.put("msg","操作失败");
			return result;
		}
		result.put("isSuc",true);
		result.put("msg","操作成功");
		return result;
	}

}
